package src.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 这一部分是JDBC的固定格式，直接拿来用的
 */
public class Dbutil {
    /**
     *      获取账号与mysql的连接
     * @return Connection对象
     */
    public static Connection getConnection(){
        Connection conn;
        String temp;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/mysql/mysql.txt")));
            temp= br.readLine();
            br.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String[] split = temp.split("&");
        String url = "jdbc:mysql://localhost:"+split[0]+"/"+split[1]+"?serverTimezone=UTC";
        try {
            conn = DriverManager.getConnection(url,split[2],split[3]);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    /**
     *     关闭与mysql的连接
     */
    public static void clossConn(Connection conn) {
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
